package cn.com.zhuxulin.info.Lists;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import cn.com.zhuxulin.info.Forms.Start;
import cn.com.zhuxulin.info.Utils.WeatherUtils;

public class ProvicesList extends List{
//	private List Provice;
//	private List City;
	private Command ok;
	private String cityID;
	private String proviceName;
	public ProvicesList(final Display display,final Start start) {
		super("Choice a Provice", List.IMPLICIT);
		ok=new Command("ok", Command.OK, 1);
//		Provice=new List("Provice", List.EXCLUSIVE);
//		Provice.append("", null);
		String[] provices;
		try {
			provices = WeatherUtils.getProvices();
			for(int i=0;i<provices.length&&!provices[i].equals("");i++){
				this.append(provices[i], null);
			}
			this.setCommandListener(new CommandListener() {
				
				public void commandAction(Command cmd, Displayable dis) {
					if(cmd==ok){
//						System.out.println(getString(getSelectedIndex()));
						cityID=getString(getSelectedIndex());
						proviceName=cityID.substring(0,cityID.indexOf(","));
						cityID=cityID.substring(cityID.indexOf(",")+1);
//						System.out.println(cityID);
						new Thread(new Runnable() {
							public void run() {
								try {
									String[] cities=WeatherUtils.getCities(cityID);
									display.setCurrent(new CitiesList(proviceName,cities,display,start));
								}catch(Exception e) {
									e.printStackTrace();
								}
							}
						}).start();
					}
//					System.out.println(cmd+"cmd click");
				}
			});
			this.setSelectCommand(ok);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
